<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.


//include_once("${root_path}ext_lib/simple_html_dom.php");


function construct_link($excludes=array("edit")){
    $link = "?";
    foreach($_GET as $parm => $value){
        if (!in_array($parm, $excludes)) {
            $link .= "$parm=$value&";
        }
    }
    return $link;
}

function str_startswith($source, $prefix){
   return strncmp($source, $prefix, strlen($prefix)) == 0;
}
function str_endswith( $str, $sub ) {
   return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}

function random_string ($numofletters) {
    if (!isset($numofletters)) $numofletters = 10; // if $numofletters is not specified sets to 10 letters
    $literki = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'W');
    $ilosc_literek = count($literki);
    for ($licz = 0; $licz < $numofletters; $licz++) {
        $rand = rand(0, $ilosc_literek-1);
        $vercode = $vercode.$literki[$rand];
    }
    return $vercode;
}

function random_file($num){
    $f = random_string(10);

    return $f;
}

function create_thumb_magick($src, $dest){
    $thumb = new Imagick($src);

    $thumb->resizeImage(100,100,Imagick::FILTER_LANCZOS,1);
    $thumb->writeImage($dest);

    $thumb->destroy();
}

function create_thumb($name,$filename){
    $new_w = 100;
    $new_h = 100;
	$system=explode(".",$name);
	if (str_endswith($name,".jpg")) $src_img=imagecreatefromjpeg($name);
	if (str_endswith($name,".JPG")) $src_img=imagecreatefromjpeg($name);
	if (str_endswith($name,".png")) $src_img=imagecreatefrompng($name);

	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);

	if ($old_x > $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y)
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	if (preg_match("/png/",$system[1]))
	{
		imagepng($dst_img,$filename);
	} else {
		imagejpeg($dst_img,$filename);
	}
	imagedestroy($dst_img);
	imagedestroy($src_img);
}

function string_encrypt($string, $key){

    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $string);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return base64_encode($encrypted_data);
}

function string_decrypt($string, $key){
    $encrypted_date = base64_decode($string);
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted_data = trim(mdecrypt_generic($td, $encrypted_date));
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    return $decrypted_data;
}

function htmlentities_ignoretags($str){

    $str = htmlspecialchars($str);
    $str = str_replace("&lt;", "<", $str);
    $str = str_replace("&gt;", ">", $str);
    $str = str_replace("&amp;", "&", $str);


    return $str;

}

function util_translate($string, $source, $target){
    require_once("${root_path}ext_lib/googleTranslate/googleTranslate.class.php");
    $gt = new GoogleTranslateWrapper();
    $result = $gt->translate($string, $target, $source);
    $result = str_replace("<html>", "", $result);
    $result = htmlentities_ignoretags($result);
    return $result;
}

function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

?>
